package userDictionaryCreator.v1_0_0;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.*;
import java.io.File;

import javax.swing.*;


public class UdcUI extends JFrame implements WindowListener
{
	public UdcUI(UserDictionaryCreator mainApp)
	{
		pMainApp = mainApp;
		initUI();
		addWindowListener(this);
	}

	//UI setup, kind of messy...i hate UIs.
	public final void initUI()
	  {
		  pane = this.getContentPane();
		  
		  pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));
		  
		  //create menu bar and menu items
		  JMenuBar menubar = new JMenuBar();
		  menubar.setAlignmentX(LEFT_ALIGNMENT);
		  JMenu file = new JMenu("File");
		  
		  //set up open menu item
		  JMenuItem oMenuItem = new JMenuItem("Open");
		  oMenuItem.setToolTipText("Load Dictionary File");
		  
		  oMenuItem.addActionListener(new ActionListener() {
			  public void actionPerformed(ActionEvent event) {
				  JFileChooser chooser = new JFileChooser();
				  chooser.showOpenDialog(pane);
				  File file = chooser.getSelectedFile();
				  try
				{
					UserDictionaryCreator.setFileName(file);
					UserDictionaryCreator.readFile();
					setFileLocationLabel(file.toString());
				} catch (Exception e)
				{
					JOptionPane.showMessageDialog(pane, "Please select a valid file!");
				}
			  }});
		  
		  //set up exit menu item
		  
		  JMenuItem eMenuItem = new JMenuItem("Exit");
	        //eMenuItem.setMnemonic(KeyEvent.VK_C);
	        eMenuItem.setToolTipText("Exit application");
	        eMenuItem.addActionListener(new ActionListener() {
	            public void actionPerformed(ActionEvent event) {
	                System.exit(0);
	            }
	        });
	            
	        //add menu items to top menu item
	        file.add(oMenuItem);
            file.add(eMenuItem);
              
            //add top menu item to menu bar
	        menubar.add(file);

	        /*create a horizontral layout box and put the menu in it. 
	        * using glue to correctly position on far left
	        * add it to top level pane
	        */
	        Box menuBox = Box.createHorizontalBox();
	        menuBox.add(menubar);
	        menuBox.add(Box.createGlue());
	        pane.add(menuBox);
	        
	        
	        /*Now I'm going to make a bunch of nested layout boxes for the rest of the UI
	         * There's going to be a horizontal box and in it will be 2 vertical boxes, 
	         * one for each text area. There will be a horizontal strut between them.
	         * */
	        
	        //set up text boxes
	        Box hBox = Box.createHorizontalBox();
	        
	        Box textBoxL = Box.createVerticalBox();
		     
	        {
				JTextArea area = new JTextArea("");
		        area.setPreferredSize(new Dimension(300, 250));
		        area.setEditable(false);
		        area.setWrapStyleWord(true);
		        area.setLineWrap(true);
		        textBoxL.add(area);
		        pLeftTextArea = area;
	        }
	        hBox.add(textBoxL);
	        
	        hBox.add(Box.createHorizontalStrut(5));
	        
	        Box textBoxR = Box.createVerticalBox();

	        {
		        JTextArea area2 = new JTextArea("");
		        area2.setPreferredSize(new Dimension(300, 250));
		        area2.setWrapStyleWord(true);
		        area2.setLineWrap(true);
		        textBoxR.add(area2);
		        pRightTextArea = area2;
	        }
	        
	        hBox.add(textBoxR);
	        this.add(hBox);
	        
	        //set up save button
	        
	        /*Finally I add another horizontal box to the top level to sit under the text boxes
	         * This bar contains a label that gives file location and a save button
	         * that allows the user to write out to disk.
	         * */
	        
	        JButton sButton = new JButton("Save");
	        sButton.setToolTipText("Save translation to dictionary");
	        sButton.addActionListener(new ActionListener() {
	        	public void actionPerformed(ActionEvent event) {
	        		String rightPanel = getRightPanelValue();
	        		if(rightPanel == null || rightPanel.equals(" "))
	        			rightPanel = "?no-translation-given?";
	        		UserDictionaryCreator.appendFile(getLeftPanelValue(), rightPanel);
	        	}

	        });
	        sButton.setMnemonic(KeyEvent.VK_S);
	        
	        JLabel fileLabel = new JLabel("File Location: ");
	        
	        JLabel fileLocationLabel = new JLabel("Please choose a file");
	        	        
	        Box bottomStrip = Box.createHorizontalBox(); 
	        bottomStrip.add(fileLabel);
	        bottomStrip.add(fileLocationLabel);
	        pFileLocationLabel = fileLocationLabel;
	        bottomStrip.add(Box.createGlue());
	        bottomStrip.add(sButton);
	        this.add(bottomStrip);
	        

		    this.pack();

	        setTitle("User Dictionary Creator");
	        setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
	        setLocationRelativeTo(null);
		  
	  }

	@Override
	public void windowActivated(WindowEvent e)
	{
	}

	@Override
	public void windowClosed(WindowEvent e)
	{
	}

	//shut down thread on window close
	@Override
	public void windowClosing(WindowEvent e)
	{
		pMainApp.new ShutdownThread().start();
		this.setVisible(true);
	}

	@Override
	public void windowDeactivated(WindowEvent e)
	{
	}

	@Override
	public void windowDeiconified(WindowEvent e)
	{
	}

	@Override
	public void windowIconified(WindowEvent e)
	{
	}

	@Override
	public void windowOpened(WindowEvent e)
	{
	}
	
	//this is the thread that updates the left panel
	public class 
	UpdateLeftPanel
	  extends Thread
	{
		public UpdateLeftPanel(String v, String t)
		{
			value = v;
			translation = t;
		}
		
		@Override
		public void run()
		{
			//sets text areas..note: right will be set to empty string if trans not found
			pLeftTextArea.setText(value);
			pLeftTextArea.invalidate();
			String s = revertStringLineBreaks(translation);
			pRightTextArea.setText(s);
		}
		
		public String revertStringLineBreaks(String s)
		{
			String newS = s.replaceAll("\\\\n", "\n");

			return newS;
		}
		
	
		private String value;
		private String translation;
	}
	
	//methods to get values in text areas
	public String getLeftPanelValue()
	{
		return pLeftTextArea.getText();
	}
	
	public String getRightPanelValue()
	{
		return pRightTextArea.getText();
	}
	
	//method to update file location label
	public void setFileLocationLabel(String fileLocation)
	{
		pFileLocationLabel.setText(fileLocation);
		pFileLocationLabel.invalidate();
	}
	
	Container pane;	
	private JTextArea pLeftTextArea;
	private JTextArea pRightTextArea;
	private JLabel pFileLocationLabel;
	private UserDictionaryCreator pMainApp;
}
